package com.catmiao.rdd.operate.transform;

import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author ChengMiao
 * @title: Transfer_01_Map
 * @projectName spark_study
 * @description: TODO
 * @date 2024/11/25 16:27
 */
public class Transfer_07_GroupBy {

    public static void main(String[] args) throws InterruptedException {

        final SparkConf conf = new SparkConf();
        conf.setAppName("appName");
        conf.setMaster("local[*]");

        final JavaSparkContext jsc = new JavaSparkContext(conf);


        ArrayList<Integer> list = Lists.newArrayList(1, 2,  3, 4);

        JavaRDD<Integer> rdd = jsc.parallelize(list, 3);


        rdd
                // 含有shuffle操作的算子可以改变分区的能力，可以指定分区数量
                .groupBy(i->i%2==0,2)
                        .collect()
                                .forEach(System.out::println);


        System.out.println("end...");

        Thread.sleep(10000 * 1000);
        jsc.close();
    }
}
